import type { PayloadAction } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';
import type { GameType, UserType } from '../../types/socketType';

const initialState: GameType = {
  status: 'unknown',
  roomName: null,
  creator: {},
  players: [],
  questions: [],
  currentQuestionId: 0,
  themeId: null,
  currentAnswers: [],
};

const gameSlice = createSlice({
  name: 'gameSlice',
  initialState,
  reducers: {
    setCreator: (state, action: PayloadAction<UserType>) => {
      state.creator = action.payload;
    },
    setGame: (state, action: PayloadAction<GameType>) => {
      state.status = action.payload.status;
      state.creator = action.payload.creator;
      state.roomName = action.payload.roomName;
      state.players = action.payload.players;
      state.questions = action.payload.questions;
      state.currentQuestionId = action.payload.currentQuestionId;
      state.themeId = action.payload.themeId;
      state.currentAnswers = action.payload.currentAnswers;
    },
  },
});

export const { setCreator, setGame } = gameSlice.actions;

export default gameSlice.reducer;
